package com.gongzhiyun.springboot3test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gongzhiyun.springboot3test.base.PageBean;
import com.gongzhiyun.springboot3test.base.R;
import com.gongzhiyun.springboot3test.entity.message.TbMessage;
import com.gongzhiyun.springboot3test.entity.message.TbPraiseReplyMessages;
import com.gongzhiyun.springboot3test.entity.message.TbReplyMessages;
import com.gongzhiyun.springboot3test.entity.user.AuthInfo;
import com.gongzhiyun.springboot3test.interceptor.AppInterceptor;
import com.gongzhiyun.springboot3test.mapper.message.TbMessageMapper;
import com.gongzhiyun.springboot3test.mapper.message.TbPraiseReplyMessagesMapper;
import com.gongzhiyun.springboot3test.mapper.message.TbReplyMessagesMapper;
import com.gongzhiyun.springboot3test.service.TbReplyMessagesService;
import com.gongzhiyun.springboot3test.vo.ParamsReply;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
@RequiredArgsConstructor
public class TbReplyMessagesServiceImpl implements TbReplyMessagesService {
    private final TbReplyMessagesMapper tbReplyMessagesMapper;
    private final TbPraiseReplyMessagesMapper tbPraiseReplyMessagesMapper;
    private final TbMessageMapper tbMessageMapper;


    /**
     * 用户回复帖子操作
     *
     * @param reply
     * @return
     */
    @Override
    public R<String> Reply(ParamsReply reply) {
        //检验回复的帖子存不存在
        TbMessage tbMessage = tbMessageMapper.selectById(reply.getMid());
        if (tbMessage == null) {
            return R.fail("帖子不存在");
        }
        //获取认证信息
        AuthInfo authInfo = AppInterceptor.get();
        Integer uid = authInfo.getUid();
        if (uid == null) {
            return R.fail("出错了");
        }

        //增加一个增加的数据
        TbReplyMessages tbReplyMessages = new TbReplyMessages();
        tbReplyMessages.setMid(reply.getMid());
        tbReplyMessages.setUid(uid);
        tbReplyMessages.setInfo(reply.getInfo());
        int i = tbReplyMessagesMapper.insert(tbReplyMessages);
        if (i != 1) {
            return R.fail("回复失败");
        }
        //更新帖子数据
        i = tbMessageMapper.addReply(reply.getMid());
        return i == 1 ? R.ok("更新信息状态成功") : R.fail("更新信息状态失败");


    }

    /**
     * 查询指定用户的回复信息
     *
     * @param uid
     * @param pageBean
     * @return
     */
    @Override
    public R<List<TbReplyMessages>> queryByUid(Integer uid, PageBean pageBean) {
        //增加查询条件
        QueryWrapper<TbReplyMessages> qw = new QueryWrapper<>();
        qw.eq("uid", uid);
        //排序
        switch (pageBean.getOrder()) {
            case 1:
                qw.orderByDesc("lastupdate");
            case 2:
                qw.orderByDesc("praise");
            default:
                qw.orderByDesc("id");
        }
        //执行分页查询
        Page<TbReplyMessages> pages = tbReplyMessagesMapper.selectPage(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), qw);
        pageBean.setTotal(pages.getTotal());
        pageBean.setPages(pages.getPages());
        //返回数据
        R<List<TbReplyMessages>> r = R.ok(pages.getRecords());
        r.setPageBean(pageBean);

        return r;
    }

    /**
     * 用户点赞帖子回复操作
     *
     * @param mrid
     * @return
     */
    @Override
    public R<String> praise(Integer mrid) {
        //检验点赞的回复存不存在
        TbReplyMessages tbReplyMessages = tbReplyMessagesMapper.selectById(mrid);
        if (tbReplyMessages == null) {
            return R.fail("该回复不存在");
        }
        //获取认证信息
        AuthInfo authInfo = AppInterceptor.get();
        Integer uid = authInfo.getUid();
        if (uid == null) {
            return R.fail("出错了");
        }
        //检验用户是否已经点赞
        QueryWrapper<TbPraiseReplyMessages> qw = new QueryWrapper<>();
        qw.eq("uid", uid);
        qw.eq("mrid", mrid);
        TbPraiseReplyMessages tbPraiseReplyMessages = tbPraiseReplyMessagesMapper.selectOne(qw);
        if (tbPraiseReplyMessages != null) {
            //已点赞，则取消点赞
            int i = tbPraiseReplyMessagesMapper.deleteById(tbPraiseReplyMessages.getId());
            if (i != 1) {
                return R.fail("操作失败");
            }
            //更新信息
            i = tbReplyMessagesMapper.DecPraise(mrid);
            return i == 1 ? R.ok("点赞成功") : R.fail("点赞失败");
        }
        //未点赞则增加点赞信息
        tbPraiseReplyMessages = new TbPraiseReplyMessages();
        tbPraiseReplyMessages.setUid(uid);
        tbPraiseReplyMessages.setMrid(mrid);
        int i = tbPraiseReplyMessagesMapper.insert(tbPraiseReplyMessages);
        if (i != 1) {
            return R.fail("操作失败");
        }
        //更新信息
        i = tbReplyMessagesMapper.addPraise(mrid);
        return i == 1 ? R.ok("点赞成功") : R.fail("点赞失败");

    }

    /**
     * 用户删除帖子回复操作
     *
     * @param mrid
     * @return
     */
    @Override
    public R<String> delete(Integer mrid) {
        //检验该帖子回复存不存在
        TbReplyMessages tbReplyMessages = tbReplyMessagesMapper.selectById(mrid);
        if (tbReplyMessages == null) {
            return R.fail("该回复不存在");

        }
        //获取认证信息
        AuthInfo authInfo = AppInterceptor.get();
        Integer uid = authInfo.getUid();
        if (uid == null) {
            return R.fail("出错了");
        }
        //检验是否是自己发布的
        if (!tbReplyMessages.getUid().equals(uid)) {
            return R.fail("只能操作自己的回复");
        }
        int i = tbReplyMessagesMapper.deleteById(mrid);
        if (i != 1) {
            return R.fail("删除信息失败");
        }
        //更新信息
        i = tbMessageMapper.DecReply(tbReplyMessages.getMid());

        return i == 1 ? R.ok("更新信息成功") : R.fail("更新信息失败");
    }

}
